require('../css/login.less')

require('../font/iconfont.css')

// 验证码
const CaptchaMini = require('../lib/captcha/captcha-mini');

// // axios
const axios = require('../lib/axios/axios.js')


document.ready(function () {
    // 表单验证

    // 获取元素
    let tel = document.querySelector('#tel');
    let yzm = document.querySelector('#inp-yzm');
    let password = document.querySelector('#password');
    let retrypw = document.querySelector('#retrypw');


    let login = document.querySelector('#login');



    // 验证码生成
    let captcha1 = new CaptchaMini({
        // lineNum:50,
        // dotR: 2,          //点的半径
        // dotNum: 85,       //点的数量
    });
    //定义一个全局变量来保存验证码
    let code = "";
    captcha1.draw(document.querySelector('#captcha1'), r => {
        // console.log('验证码:', r);
        code = r;

    });


    let tips = document.querySelector('#tips');
    // 登录
    login.addEventListener('click', function () {

        const t = tel.value;
        const y = yzm.value;
        const p = password.value;
        const rp = retrypw.value;

        if (!(t && y && p && rp)) {
            $createMask("icon-dingwei1", "输入框不能空", 1500)
            return
        }

        if (!$isPhone(t)) {
            tips.innerHTML = "请输入正确的手机号码!"

        } else if (!y.toUpperCase() === code.toUpperCase()) {
            tips.innerHTML = "验证码错误!"

        } else if (!$isPassword(p)) {
            tips.innerHTML = "密码格式错误,请输入6-12位数字或字母!"

        } else if (rp !== p) {
            tips.innerHTML = "两次密码输入不一致!"
        } else {
            // 都正确
            tips.innerHTML = ""

            axios.post(BASE_URL + "/users/add", {
                "account": t,
                "password": p
            }).then((res) => {

                if (res.data.status == 0 && res.data.msg == "OK") {
                    $createMask("icon-dingwei1", "操作成功", 1500, function () {
                        location.href = './register.html'
                    })
                } else {
                     $createMask("icon-dingwei1", "登录失败!", 1500)
                }
            }).catch(r => {
                 $createMask("icon-dingwei1", "网络错误", 1500)
            })
        }
    })


    // 密码登录
    let psLogin = document.querySelector('#ps-login');
    psLogin.addEventListener('click', function () {
        location.href = "./register.html"
    })


})